package com.agy.filemanager.common;

import com.agy.filemanager.entity.Control;
import com.agy.filemanager.entity.ProExpandInfo;
import com.agy.filemanager.exception.FieldMismatchException;
import org.apache.commons.lang3.StringUtils;
import org.mortbay.util.ajax.JSON;

import java.io.Serializable;
import java.util.Map;

public class DynamicTagVo implements Serializable {
    private final static int DEFAULT_HEIGHT = 30;
    private final static int DEFAULT_WIDTH = 12;
    private final static String WIDTH_PREFIX = "col-md-";
    //扩展字段id
    private String id;
    //控件名称
    private String tagName;
    //字段名称
    private String columnName;
    //字段别名
    private String alias;
    //表名称
    private String tableName;
    //js路径
    private String jsPath;
    //高度
    private String height;
    //宽度
    private String width;
    //是否多选
    private boolean isMult;
    //是否必填
    private boolean isRequired;
    //表达式
    //排序
    private int sort;
    //数据格式
   private ExpressionMap expressionMap;
   //数据认证
    private String regular;

    private DynamicTagVo(){
    }

    public DynamicTagVo(ProExpandInfo proExpandInfo, Control control,ExpressionFormatMapping mapping) {
        setDefaultFiled(proExpandInfo,control);
        setHeightWidth(proExpandInfo.getControlType());
        ExpressionMap expressionMap = mapping.executeFormat(proExpandInfo.getWait());
        String regularFormat = mapping.regularFormat(proExpandInfo.getDataFormat());
        //设置数据
        this.expressionMap = expressionMap;
        //设置正则
        this.regular = regularFormat;
    }

    /**
     * 设置基础信息
     * @param proExpandInfo
     * @param control
     */
    private void setDefaultFiled(ProExpandInfo proExpandInfo, Control control){
        this.id  = proExpandInfo.getId();
        this.tagName = proExpandInfo.getName();
        this.columnName = proExpandInfo.getColumnName();
        this.alias = proExpandInfo.getAlias();
        this.tableName = proExpandInfo.getAppendixName();
        this.jsPath = control.getJsMethod();
        this.isMult = control.getIsMult();
        this.isRequired = proExpandInfo.getIswrite();
        this.sort = proExpandInfo.getSort();
    }

    /**
     * 设置高度和宽度
     * @param controlType
     */
    private void setHeightWidth(String controlType){
        if(StringUtils.isNotBlank(controlType)){
            this.height = this.DEFAULT_HEIGHT + "px";
            this.width = this.WIDTH_PREFIX + this.DEFAULT_WIDTH;
        }else{
            Map<String , Integer> styleMap = (Map) JSON.parse(controlType);
            Integer h = styleMap.get("height");
            Integer w = styleMap.get("width");
            this.height = this.DEFAULT_HEIGHT * h + "px";
            this.width = this.WIDTH_PREFIX + (this.DEFAULT_WIDTH * w);
        }
    }

    /**
     * 获取默认DynamicTagVo实例
     * @param proExpandInfo
     * @param control
     * @return
     */
    public static  DynamicTagVo getInstance(ProExpandInfo proExpandInfo, Control control){
        if(proExpandInfo == null || control == null ){
            return null;
        }
        if (proExpandInfo.getControlType().equals(control.getId())){
            throw  new FieldMismatchException("ProExpandInfo:the controlType doesn't match the Control ");
        }
        DynamicTagVo dynamicTagVo = new DynamicTagVo(proExpandInfo,control,new ExpressionFormatMappingAdaptor());
        return  dynamicTagVo;
    }

    @Override
    public int hashCode() {
        return this.id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof  DynamicTagVo)) return  false;
        DynamicTagVo tagVo = (DynamicTagVo) obj;
        return this.id.equals(tagVo.getId());
    }

    public String getId() {
        return id;
    }

    public String getTagName() {
        return tagName;
    }

    public String getColumnName() {
        return columnName;
    }

    public String getAlias() {
        return alias;
    }

    public String getTableName() {
        return tableName;
    }

    public String getJsPath() {
        return jsPath;
    }

    public String getHeight() {
        return height;
    }

    public String getWidth() {
        return width;
    }
    public boolean isMult() {
        return isMult;
    }
}
